/**
 * 
 */
package sudoku.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import sudoku.util.BrowserControl;
import sudoku.util.Language;


/**
 * @author Romet
 *
 */
public class AboutUI implements ActionListener, MouseListener{
	
	private volatile SudokuUI m2ng;
	private volatile Language language;
	
	private JLabel link1;
	private JLabel link2;
	private JDialog dialog;
	private JButton ok;
	
	/**
	 * Teeb mängu taustainformatsiooni akna
	 * @param m2ng põhiakna graafika klassi isend
	 */
	public AboutUI(Language language, SudokuUI m2ng){
		this.language = language;
		this.m2ng = m2ng;
		
		drawWindow();
	}
	
	public void actionPerformed(ActionEvent e) {
		dialog.dispose();
	}

	public void drawWindow() {
		dialog = new JDialog(m2ng, language.getMessage(), true);
		dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		JPanel panel = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		int i = 0;
		int link = 1;
		String[] s = language.getAbout_message().split("\n");
		for (; i < s.length; i++) {
			c.gridx = 0;
			c.gridy = i;
			c.anchor = GridBagConstraints.LINE_START;
			JLabel label = new JLabel(s[i]);
			if (s[i].startsWith(" http")) {
				if (link == 1) {
					link1 = label;
					link1.setForeground(Color.BLUE);
					link1.addMouseListener(this);
					panel.add(link1, c);
				}
				else if (link == 2) {
					link2 = label;
					link2.setForeground(Color.BLUE);
					link2.addMouseListener(this);
					panel.add(link2, c);
				}
				link++;
			}
			else
				panel.add(label, c);
		}
		
		c.gridx = 0;
		c.gridy = i;
		c.anchor = GridBagConstraints.CENTER;
		ok = new JButton("OK");
		ok.addActionListener(this);
		panel.add(ok, c);
		dialog.add(panel, BorderLayout.CENTER);
		dialog.pack();
		dialog.setLocation(m2ng.getLocation().x+m2ng.getWidth()/2-dialog.getWidth()/2, m2ng.getLocation().y+m2ng.getHeight()/2-dialog.getHeight()/2);
		dialog.setVisible(true);
	}

	public void mouseClicked(MouseEvent e) {
		if (e.getSource().equals(link1)) {
			BrowserControl.displayURL(link1.getText().substring(1));
			dialog.dispose();
		}
		else if (e.getSource().equals(link2)) {
			BrowserControl.displayURL(link2.getText().substring(1));
			dialog.dispose();
		}
	}

	public void mouseEntered(MouseEvent e) {
		dialog.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
	}

	public void mouseExited(MouseEvent e) {
		dialog.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
	}

	public void mousePressed(MouseEvent e) {
	}

	public void mouseReleased(MouseEvent e) {
	}
}
